package com.example.day1203_newsport.slice;

import com.example.day1203_newsport.Dialog.PopupWindow;
import com.example.day1203_newsport.ResourceTable;
import com.example.day1203_newsport.bean.Data;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Image;
import ohos.agp.components.webengine.WebView;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.List;

public class NewsPageInfo extends AbilitySlice implements IAbilityContinuation {

    private Image mCollection;
    private Image mPerasie;
    private Image mShare;
    //是否 收藏
    private boolean isCollection;
    // 是否点赞
    private boolean isPerasie;

    private String mUrl;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_news_page_info);

        WebView webView = (WebView) findComponentById(ResourceTable.Id_web_view);

        Data data = intent.getSerializableParam("key_data");

        if (data != null) {
            mUrl = data.getUrl();
        }

        webView.load(mUrl);
        // 初始化组件
        initCompent();
    }

    private void initCompent() {

        mCollection = findComponentById(ResourceTable.Id_img_collection);
        mPerasie = findComponentById(ResourceTable.Id_img_perasie);
        mShare = findComponentById(ResourceTable.Id_img_share);

        mCollection.setClickedListener(component -> onCollection());
        mPerasie.setClickedListener(component -> onPearsie());
        mShare.setClickedListener(component -> onShare());

    }

    /**
     * 分享
     */
    private void onShare() {
        List<DeviceInfo> deviceList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        PopupWindow popupWindow = new PopupWindow(getContext(), deviceList);

        popupWindow.setPopupWindowCallback(new PopupWindow.PopupWindowCallback() {
            @Override
            public void result(DeviceInfo deviceInfo) {
                // 获取设备id 并进行跨设备迁移
                continueAbility(deviceInfo.getDeviceId());
                // 迁移后 关闭进行迁移的设备页面
                terminateAbility();
            }
        });

        popupWindow.show();

    }

    /**
     * 点赞
     */
    private void onPearsie() {
        isPerasie = !isPerasie;
        int peixmap = isPerasie ? ResourceTable.Media_icon_dianzan_red : ResourceTable.Media_icon_dianzan;
        mPerasie.setPixelMap(peixmap);
    }


    /**
     * 收藏
     */
    private void onCollection() {
        isCollection = !isCollection;
        int pexmap = isCollection ? ResourceTable.Media_icon_shoucang_red : ResourceTable.Media_icon_shoucang;
        mCollection.setPixelMap(pexmap);
    }

    @Override
    public boolean onStartContinuation() {

        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        intentParams.setParam("key_url", mUrl);
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        mUrl = (String) intentParams.getParam("key_url");
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {

    }
}
